package com.saber.util;

import java.util.Random;
import java.util.stream.Collectors;

import com.google.common.base.Optional;

public class ToolUtils {

	/**
	 * 默认随机个数
	 */
	private final static int DEFAULT_RANDOM_STREAMSIZE = 10;

	/**
	 * 默认随机下限
	 */
	private final static int DEFAULT_RANDOM_ORIGIN = 1;

	/**
	 * 取得随机字符（a-Z）
	 *
	 * @param size
	 *            随机个数
	 * @return 限定的随机字符
	 */
	public static String randomStr(Optional<Integer> size) {
		String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
		Random rd = new Random();
		int streamSize = size.isPresent() ? size.get() : DEFAULT_RANDOM_STREAMSIZE;
		return rd.ints(streamSize, DEFAULT_RANDOM_ORIGIN, str.length()).mapToObj(index -> {
			return String.valueOf(str.charAt(index));
		}).collect(Collectors.joining(""));
	}

	/**
	 * 取得随机字符（a-Z）
	 *
	 * @return 限定的随机字符
	 */
	public static String randomStr() {
		String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
		Random rd = new Random();
		return rd.ints(DEFAULT_RANDOM_STREAMSIZE, DEFAULT_RANDOM_ORIGIN, str.length()).mapToObj(index -> {
			return String.valueOf(str.charAt(index));
		}).collect(Collectors.joining(""));
	}
}
